package com.wing.product.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.chanjet.entity.inventory.BaseResponse;
import com.wing.chanjet.entity.inventory.InventoryClassResponse;
import com.wing.chanjet.entity.inventory.InventoryResponse;
import com.wing.chanjet.feign.ChanjetClient;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.dict.model.entity.DictLabel;
import com.wing.dict.service.DictLabelService;
import com.wing.platform.feign.PlatformClient;
import com.wing.product.dao.ProductDetailDao;
import com.wing.product.model.entity.*;
import com.wing.product.model.response.ProductAttributeVO;
import com.wing.product.model.response.ProductItemVO;
import com.wing.product.model.response.ProductSpecVO;
import com.wing.product.model.response.ProductUnitInfoVO;
import com.wing.purchase.model.entity.SupplierInfo;
import com.wing.purchase.service.SupplierInfoService;
import com.wing.sell.model.entity.OrderPro;
import com.wing.sell.service.OrderProService;
import com.wing.sell.service.OrderService;
import com.wing.web.excel.ProductDetailExcelIn;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 产品明细表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProductDetailServiceImpl extends ServiceImpl<ProductDetailDao, ProductDetail> implements ProductDetailService {
    @Autowired
    private ProductDetailDao productDetailDao;

    @Autowired
    private PlatformClient platformClient;

    @Autowired
    private ProductCategoryExtService productCategoryExtService;

    @Autowired
    private DictLabelService dictLabelService;

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private ProductBrandService productBrandService;

    @Autowired
    private SupplierInfoService supplierInfoService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private ProcessLineTemplateService processLineTemplateService;

    @Autowired
    private ProcessLineTemplateRecordService processLineTemplateRecordService;

    @Autowired
    private ProductUnitService productUnitService;

    @Autowired
    private ProductSpecService productSpecService;

    @Autowired
    private ProductAttributeService productAttributeService;

    @Autowired
    private ProductItemService productItemService;

    @Autowired
    ProductDetailAttributeService productDetailAttributeService;

    @Autowired
    ProductDetailSpecService productDetailSpecService;

    @Resource
    OrderProService orderProService;

    @Override
    public int add(ProductDetail productDetail) {
        return baseMapper.insert(productDetail);
    }

    @Override
    public List<ProductDetail> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductDetail> pageList4app(Page page, Map<String, Object> params) {
        List<ProductDetail> list = baseMapper.list4app(page, params);
        Long orderId = params.get("orderId") == null ? null : Long.valueOf(params.get("orderId").toString());
        if (orderId != null) {
            //订单产品列表
            List<OrderPro> orderProList = orderProService.list();
            for (ProductDetail productDetail : list) {
                OrderPro orderPro = orderProList.stream()
                        .filter(t -> t.getOrderId().equals(orderId) &&
                                t.getProductDetailId().longValue() == productDetail.getId().longValue())
                        .findFirst().orElse(null);
                if (ObjectUtil.isNotEmpty(orderPro)) {
                    productDetail.setOrderId(orderPro.getOrderId());
                    productDetail.setOrderProId(orderPro.getId());
                    productDetail.setOrderProNum(orderPro.getQuantity());
                }
                //查询该物料剩余的和已排产的
                Map<String, Object> map = this.orderProService.getNeedInfoBy(productDetail.getSkuCode(), orderId);
                if (ObjectUtil.isNotNull(map)) {
                    productDetail.setHaveQuantityNum(Integer.valueOf(map.get("haveQuantityNum") == null ? "0" : map.get("haveQuantityNum").toString()));
                    productDetail.setNoQuantityNum(Integer.valueOf(map.get("noQuantityNum") == null ? "0" : map.get("noQuantityNum").toString()));
                }

            }
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductDetail get4appById(Long id) {
        //物料明细
        ProductDetail productDetail = baseMapper.get4appById(id);
        //计量单位列表
        Map productTypeMap = new HashMap();
        productTypeMap.put("productType", productDetail.getProductTypeId());
        List<ProductUnitInfoVO> productUnitList = productUnitService.list4app(productTypeMap);
        productDetail.setProductUnitList(productUnitList);
        //规格列表
        Map productSpecMap = new HashMap();
        productSpecMap.put("productType", productDetail.getProductTypeId());
        List<ProductSpecVO> productSpecList = productSpecService.list4app(productSpecMap);
        productDetail.setProductSpecList(productSpecList);
        //属性列表
        Map productAttributeMap = new HashMap();
        productAttributeMap.put("productType", productDetail.getProductTypeId());
        List<ProductAttributeVO> productAttributeList = productAttributeService.list4app(productAttributeMap);
        productDetail.setProductAttributeList(productAttributeList);
        //检测项列表
        Map productItemMap = new HashMap();
        productItemMap.put("productType", productDetail.getProductTypeId());
        List<ProductItemVO> productItemList = productItemService.list4app(productItemMap);
        productDetail.setProductItemList(productItemList);
        //产品规格值列表
        QueryWrapper<ProductDetailSpec> specQueryWrapper = new QueryWrapper<>();
        specQueryWrapper.eq("product_detail_id", productDetail.getId());
        List<ProductDetailSpec> productDetailSpecList = productDetailSpecService.list(specQueryWrapper);
        productDetail.setProductDetailSpecList(productDetailSpecList);
        //规格属性值列表
        QueryWrapper<ProductDetailAttribute> attrQueryWrapper = new QueryWrapper<>();
        attrQueryWrapper.eq("product_detail_id", productDetail.getId());
        List<ProductDetailAttribute> productDetailAttrList = productDetailAttributeService.list(attrQueryWrapper);
        productDetail.setProductDetailAttributeList(productDetailAttrList);
        return productDetail;
    }

    @Override
    public void deleteProductDetailByProductId(Long productId) {
        QueryWrapper<ProductDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", productId);
        baseMapper.delete(wrapper);
    }

    @Override
    public ProductDetail getProductDetailBySkuCode(String skuCode) {
        ProductDetail productDetail = productDetailDao.getProductDetailBySkuCode(skuCode);
        return productDetail;
    }

    @Override
    public IPage<ProductDetail> getProductDetailPageList(ProductDetail productDetail, Integer page, Integer limit) {
        IPage<ProductDetail> iPage = new Page<ProductDetail>(page, limit);
        QueryWrapper<ProductDetail> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(productDetail.getSkuCode())) {
            wrapper.and(qw -> qw.like("pd.sku_code", productDetail.getSkuCode()));
        }
        if (productDetail.getProductTypeId() != null) {
            wrapper.eq("p.product_type_id", productDetail.getProductTypeId());
        }
        if (productDetail.getProductCategoryId() != null) {
            wrapper.eq("p.product_category_id", productDetail.getProductCategoryId());
        }
        productDetailDao.getProductDetailPageList(iPage, wrapper);
        return iPage;
    }

    @Override
    public IPage<ProductDetail> getInventoryProductDetailPageList(Integer page, Integer limit, String skuName, String skuCode
            , Long productTypeId, Long id, Integer type) {
        IPage<ProductDetail> iPage = new Page<ProductDetail>(page, limit);
        QueryWrapper<ProductDetail> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(skuName)) {
            wrapper.and(qw -> qw.like("t.sku_name", skuName));
        }
        if (StringUtils.isNotEmpty(skuCode)) {
            wrapper.and(qw -> qw.like("t.sku_code", skuCode));
        }
        if (productTypeId != null) {
            wrapper.eq("t.product_type_id", productTypeId);
        }
        wrapper.orderByDesc("t.create_time  ");
        productDetailDao.getInventoryProductDetailPageList(iPage, wrapper);
        return iPage;
    }

    @Override
    public void importExcel(List<ProductDetailExcelIn> list) {
        //工艺线路模板列表
        List<ProcessLineTemplate> processLineTemplateList = processLineTemplateService.list();
        //工艺线路-工序列表
        List<ProcessLineTemplateRecord> processLineTemplateRecordList = processLineTemplateRecordService.list();
        //货品信息列表
        List<Product> productList = productService.list();
        //物料分类列表
        QueryWrapper<ProductCategory> productCategoryQueryWrapper = new QueryWrapper<>();
        productCategoryQueryWrapper.eq("is_final", 1);
        List<ProductCategory> productCategoryList = productCategoryService.list(productCategoryQueryWrapper);
        //所有物料信息
        List<ProductDetail> productDetailAllList = this.baseMapper.selectList(null);
        //物料类别
        QueryWrapper<ProductType> productTypeQueryWrapper = new QueryWrapper<>();
//        productTypeQueryWrapper.eq("final_classify_status", 0);
        List<ProductType> productTypeList = productTypeService.list(productTypeQueryWrapper);
        //品牌
        QueryWrapper<ProductBrand> productBrandQueryWrapper = new QueryWrapper<>();
        List<ProductBrand> productBrandList = productBrandService.list(productBrandQueryWrapper);
        //供应商
        QueryWrapper<SupplierInfo> supplierQueryWrapper = new QueryWrapper<>();
        List<SupplierInfo> supplierList = supplierInfoService.list(supplierQueryWrapper);
        //字典数据
        List<DictLabel> dictLabelList = dictLabelService.list(null);
        //(主)计量单位列表
        List<ProductUnitInfo> productUnitList = productUnitService.list(null);

        List<ProductDetail> productDetailList = new ArrayList<>();

        int i = 1;
        for (ProductDetailExcelIn productDetail : list) {
            if (StringUtils.isBlank(productDetail.getSkuName())) {
                throw new BusinessException(300, "第" + i + "行【料号名称】未输入数据");
            }
            if (StringUtils.isBlank(productDetail.getSkuCode())) {
                throw new BusinessException(300, "第" + i + "行【料号】未输入数据");
            }
            if (StringUtils.isBlank(productDetail.getProductCode())) {
                throw new BusinessException(300, "第" + i + "行【货品编码】未输入数据");
            }
            if (StringUtils.isBlank(productDetail.getSpecCode())) {
                throw new BusinessException(300, "第" + i + "行【规格码】未输入数据");
            }
            if (StringUtils.isBlank(productDetail.getMaterialNature())) {
                throw new BusinessException(300, "第" + i + "行【物料类别性质】未输入数据");
            }
            if (StringUtils.isBlank(productDetail.getProductType())) {
                throw new BusinessException(300, "第" + i + "行【所属类别】未输入数据");
            }
            if (StringUtils.isBlank(productDetail.getProductCategory())) {
                throw new BusinessException(300, "第" + i + "行【物料分类】未输入数据");
            }
            if (StringUtils.isBlank(productDetail.getCalculateUnit())) {
                throw new BusinessException(300, "第" + i + "行【计量单位】未输入数据");
            }
            if (StringUtils.isBlank(productDetail.getCalculateUnit())) {
                throw new BusinessException(300, "第" + i + "行【基础单位】未输入数据");
            }
            i++;

            //判断料号是否存在
            ProductDetail productDetail0 = productDetailAllList.stream().filter(t -> t.getSkuCode().equals(productDetail.getSkuCode())).findFirst().orElse(null);
            if (productDetail0 != null) {
                throw new BusinessException(300, "料号[" + productDetail.getSkuCode() + "]已存在");
            }
            ProductDetail detail = new ProductDetail();
            detail.setSkuName(productDetail.getSkuName());
            detail.setSkuCode(productDetail.getSkuCode());
            //货品编码
            Product product = productList.stream().filter(t -> t.getProductCode().equals(productDetail.getProductCode())).findFirst().orElse(null);
            if (product != null) {
                detail.setProductId(product.getId());
            } else {
                //创建货品信息--待定
            }
            detail.setStandardBarCode(productDetail.getBarCode());
            detail.setHistoryProductName(productDetail.getHistoryProductName());
            detail.setOldSkuCode(productDetail.getHistorySkuCode());
            detail.setSkuSpec(productDetail.getSpecCode());
            //物料类别性质
            DictLabel materialNature = dictLabelList.stream().filter(t -> "sys_product_type_nature".equals(t.getDictType()) && productDetail.getMaterialNature().equals(t.getLabelName())).findFirst().orElse(null);
            detail.setMaterialNature(materialNature.getLabelValue());
            //所属类别
            ProductType productType = productTypeList.stream().filter(t -> productDetail.getProductType().equals(t.getName())).findFirst().orElse(null);
            if (ObjectUtil.isNotEmpty(productType)) {
                //设置主单位 随机一个
                assert productType != null;
                detail.setProductTypeId(productType.getId());
                ProductUnitInfo productUnit = productUnitList.stream().
                        filter(t -> t.getProductType().equals(productType.getId())
                                && t.getName().equals(productDetail.getCalculateUnit()))
                        .findFirst()
                        .orElse(null);
                if (productUnit != null) {
                    //默认一
                    detail.setMainCalWay(productUnit.getCalWay());
                    detail.setMainUnitId(productUnit.getId());
                    detail.setMainUnitRate("1");
                }

                //基础类型
                ProductUnitInfo baseInfo = productUnitList.stream().
                        filter(t -> t.getProductType().equals(productType.getId())
                                && t.getIsBaseUnit())
                        .findFirst()
                        .orElse(null);
                if (ObjectUtil.isNotEmpty(baseInfo)) {
                    detail.setBaseUnit(baseInfo.getId());
                }
            }


            //物料分类
            ProductCategory productCategory = productCategoryList.stream().filter(t -> t.getName().equals(productDetail.getProductCategory())).findFirst().orElse(null);
            if (ObjectUtil.isNotEmpty(productCategory)) {
                detail.setProductCategoryId(productCategory.getId());
            }
            //品牌
            if (ObjectUtil.isNotEmpty(productDetail.getBrandName())) {
                ProductBrand productBrand = productBrandList.stream().filter(t -> productDetail.getBrandName().equals(t.getName())).findFirst().orElse(null);
                if (ObjectUtil.isNotEmpty(productBrand)) {
                    detail.setBrandId(productBrand.getId());
                }
            }
            //计价币种
            if (StrUtil.isNotEmpty(productDetail.getPriceCurrency())) {
                DictLabel priceCurrency = dictLabelList.stream().filter(t -> "sys_product_detail_price_currency".equals(t.getDictType()) && productDetail.getPriceCurrency().equals(t.getLabelName())).findFirst().orElse(null);
                detail.setPriceCurrency(priceCurrency.getLabelValue());
            }
            detail.setTaxRate(productDetail.getTaxRate());
            detail.setSelfProductStatus("是".equals(productDetail.getSelfProductStatus()) ? "0" : "1");
            //产品性质
            DictLabel productNature = dictLabelList.stream().filter(t -> "product_nature".equals(t.getDictType()) && productDetail.getProductNature().equals(t.getLabelName())).findFirst().orElse(null);
            detail.setProductNature(productNature.getLabelValue());
            detail.setOutsourceStatus("是".equals(productDetail.getOutsourceStatus()) ? "0" : "1");
            //工艺线路
            if (StringUtils.isNotBlank(productDetail.getProductProcessLine())) {
                ProcessLineTemplate processLineTemplate = processLineTemplateList.stream().filter(t -> t.getName().equals(productDetail.getProductProcessLine())).findFirst().orElse(null);
                detail.setProductProcessLineId(processLineTemplate.getId());
                //SKU工序集
                List<ProcessLineTemplateRecord> recordList = processLineTemplateRecordList.stream().filter(t -> t.getProcessLineTemplateId().longValue() == processLineTemplate.getId()).collect(Collectors.toList());
                String skuProcessCode = "";
                for (int k = 0; k < recordList.size(); k++) {
                    if (k == 0) {
                        skuProcessCode = recordList.get(k).getProcessId() + "";
                    } else {
                        skuProcessCode += "," + recordList.get(k).getProcessId();
                    }
                }
                detail.setSkuProcessCode(skuProcessCode);
            }
            detail.setCostPrice(productDetail.getCostPrice() == null ? BigDecimal.ZERO : new BigDecimal(productDetail.getCostPrice()));
            detail.setSellStatus("是".equals(productDetail.getSellStatus()) ? "0" : "1");
            detail.setMarketPrice(productDetail.getSellPrice() == null ? BigDecimal.ZERO : new BigDecimal(productDetail.getSellPrice()));
            detail.setLength(productDetail.getLength());
            detail.setWidth(productDetail.getWidth());
            detail.setHeight(productDetail.getHeight());
            detail.setWeight(productDetail.getWeight());
            detail.setWarningQuantity(productDetail.getSafeStockValue() == null ? 0 : Integer.valueOf(productDetail.getSafeStockValue()));
            detail.setPurchaseCycle(productDetail.getPurchaseCycle() == null ? 0 : Integer.valueOf(productDetail.getPurchaseCycle()));
            detail.setLethargyThreshold(productDetail.getDullTimeValue() == null ? 0 : Integer.valueOf(productDetail.getDullTimeValue()));
            detail.setWarrantyDay(productDetail.getWarrantyDay() == null ? 0 : Integer.valueOf(productDetail.getWarrantyDay()));
            detail.setDepreciationTime(productDetail.getDepreciationTime() == null ? 0 : Integer.valueOf(productDetail.getDepreciationTime()));
            //数据来源
            DictLabel dataSource = dictLabelList.stream().filter(t -> "sys_product_detail_data_source".equals(t.getDictType()) && productDetail.getDataSource().equals(t.getLabelName())).findFirst().orElse(null);
            detail.setDataSource(dataSource.getLabelValue());
            detail.setThingCodeStatus("是".equals(productDetail.getThingCodeName()) ? "0" : "1");
            detail.setBatchStatus("是".equals(productDetail.getBatchName()) ? "0" : "1");
            detail.setInoutStatus("是".equals(productDetail.getInoutName()) ? "0" : "1");
            //助记码
            detail.setMnemonicCode(productDetail.getMnemonicCode());
            //成本核算方式
            if (ObjectUtil.isNotEmpty(productDetail.getCostCheckType())) {
                DictLabel costCheckType = dictLabelList.stream().filter(t -> "product_detail_cost_check_type".equals(t.getDictType()) && productDetail.getCostCheckType().equals(t.getLabelName())).findFirst().orElse(null);
                detail.setCostCheckType(costCheckType.getLabelValue());
            }
            //参考成本
            if (ObjectUtil.isNotEmpty(productDetail.getReferenceCostPrice())) {
                detail.setReferenceCostPrice(productDetail.getReferenceCostPrice() == null ? BigDecimal.ZERO : new BigDecimal(productDetail.getReferenceCostPrice()));
            }
            //最新成本
            if (ObjectUtil.isNotEmpty(productDetail.getCurrentCostPrice())) {
                detail.setCurrentCostPrice(productDetail.getCurrentCostPrice() == null ? BigDecimal.ZERO : new BigDecimal(productDetail.getCurrentCostPrice()));

            }
            if (ObjectUtil.isNotEmpty(productDetail.getRemark())) {
                detail.setRemark(productDetail.getRemark());
            }
            productDetailList.add(detail);
        }
        this.saveBatch(productDetailList);
    }

    @Override
    public ProductDetail getProductDetailBySkuCodeNo(String skuCode) {
        return productDetailDao.getProductDetailBySkuCodeNo(skuCode);
    }

    @Resource
    ChanjetClient chanjetClient;

    @Override
    public void chanjetInventorySync() {
        //物料列表
        List<ProductDetail> list = this.list();
        //存货分类列表
        List<ProductCategory> productCategoryList = productCategoryService.list();
        //存货列表
        JsonResult<List<InventoryResponse>> jsonResult = chanjetClient.inventoryQuery();

        List<ProductDetail> productDetailList = new ArrayList<>();

        if (jsonResult.getCode() == 200) {
            List<InventoryResponse> inventoryList = jsonResult.getData();
            for (InventoryResponse inventory : inventoryList) {
                ProductDetail detail = new ProductDetail();
                detail.setSkuCode(inventory.getCode());
                detail.setSkuName(inventory.getName());
                detail.setSkuSpec(inventory.getSpecification());
                detail.setStandardBarCode(inventory.getDefaultBarCode());
                //存货分类
                ProductCategory productCategory = productCategoryList.stream().filter(t -> t.getName().equals((inventory.getInventoryClass().getCode() + "-" + inventory.getInventoryClass().getName()))).findFirst().orElse(null);
                if (productCategory != null) {
                    detail.setProductCategoryId(productCategory.getId());
                }
                detail.setMnemonicCode(inventory.getShorthand());
                detail.setReferenceCostPrice(StringUtils.isBlank(inventory.getInvSCost()) ? BigDecimal.ZERO : new BigDecimal(inventory.getInvSCost()));
                detail.setCurrentCostPrice(StringUtils.isBlank(inventory.getLatestCost()) ? BigDecimal.ZERO : new BigDecimal(inventory.getLatestCost()));
                detail.setMarketPrice(StringUtils.isBlank(inventory.getRetailPriceNew()) ? BigDecimal.ZERO : new BigDecimal(inventory.getRetailPriceNew()));
                detail.setCreateTime(LocalDateTime.now());
                detail.setCreator(JwtUtils.getLoginUserId());
                detail.setCreatorName(JwtUtils.getLoginUsername());
                detail.setRemark(JSON.toJSONString(inventory.getUnit()));
                ProductDetail productDetail = list.stream().filter(t -> t.getSkuCode().equals(detail.getSkuCode())).findFirst().orElse(null);
                if (productDetail != null) {
                    detail.setId(productDetail.getId());
                }
                productDetailList.add(detail);
            }
            if (!productDetailList.isEmpty() && productDetailList.size() > 0) {
                this.saveOrUpdateBatch(productDetailList);
            }
        }
    }

    @Override
    public void productDetailBind(Long productTypeId, List<String> productDetailIds) {
        List<ProductDetail> list = new ArrayList<>();
        //物料列表
        QueryWrapper<ProductDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("product_type_id");
        List<ProductDetail> productDetailList = this.list(queryWrapper);
        //物料类别
        ProductType productType = productTypeService.getById(productTypeId);
        if (productType == null) {
            throw new BusinessException("参数错误，物料类别不存在");
        }
        for (String productDetailId : productDetailIds) {
            ProductDetail productDetail = productDetailList.stream().filter(t -> t.getId().longValue() == Long.valueOf(productDetailId)).findFirst().orElse(null);
            if (productDetail == null) {
                throw new BusinessException("参数错误，物料信息不存在");
            }
            String unitStr = productDetail.getRemark();
            BaseResponse unit = JSON.parseObject(unitStr, BaseResponse.class);
            //物料单位列表
            QueryWrapper<ProductUnitInfo> unitQueryWrapper = new QueryWrapper<>();
            unitQueryWrapper.eq("product_type", productTypeId);
            unitQueryWrapper.eq("code", unit.getCode());
            unitQueryWrapper.eq("name", unit.getName());
            ProductUnitInfo productUnit = productUnitService.getOne(unitQueryWrapper);
            if (productUnit == null) {
                productUnit = new ProductUnitInfo();
                productUnit.setProductType(productTypeId);
                productUnit.setCode(unit.getCode());
                productUnit.setName(unit.getName());
                productUnit.setSortNumber(1);
                productUnit.setType(ProductUnitInfo.Type.knife);
                productUnit.setIsBaseUnit(false);
                productUnit.setDataSourceType(ProductUnitInfo.DataSourceType.enterprise);
                productUnit.setCalWay("2");
                productUnit.setCreateTime(LocalDateTime.now());
                productUnit.setCreator(JwtUtils.getLoginUserId());
                productUnit.setCreatorName(JwtUtils.getLoginUsername());
                productUnitService.save(productUnit);
            }
            productDetail.setProductTypeId(productTypeId);
            productDetail.setBaseUnit(productUnit.getId());
            productDetail.setMainCalWay("2");
            productDetail.setMainUnitId(productUnit.getId());
            productDetail.setMainUnitRate("1");
            productDetail.setSubCalWay("2");
            productDetail.setSubUnitId(productUnit.getId());
            productDetail.setSubUnitRate("1");
            productDetail.setRemark("");
            list.add(productDetail);
        }
        if (!list.isEmpty() && list.size() > 0) {
            this.saveOrUpdateBatch(list);
        }
    }
}
